package Offer.q06_reversePrint;


import CommonClass.Common;
import CommonClass.Common.ListNode;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

public class Solution {
    public static void main(String[] args) {
        ListNode ln = new ListNode();
        ln = Common.generateList(new int[] {
            1, 2, 3
        });
        Solution s = new Solution();
        int[] arr = s.reversePrint(ln);
        System.out.println(Arrays.toString(arr));
    }

    public int[] reversePrint(ListNode head) {
        Deque<Integer> stack = new LinkedList<Integer>();
        storeInt(head, stack);

        // System.out.println(stack);
        int[] res = new int[stack.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = stack.pop();
        }
        return res;
    }


    private void storeInt(ListNode head, Deque<Integer> stack) {
        if (head == null){
            return;
        }
        stack.push(head.val);
        storeInt(head.next, stack);
    }
}
